// API 配置
export const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:3001';
export const WS_URL = import.meta.env.VITE_WS_URL || 'ws://localhost:3001';

// WebRTC 配置
export const RTC_CONFIGURATION: RTCConfiguration = {
  iceServers: [
    {
      urls: 'stun:stun.l.google.com:19302'
    },
    {
      urls: 'stun:stun1.l.google.com:19302'
    },
    // 生产环境需要配置 TURN 服务器
    // {
    //   urls: 'turn:your-turn-server.com:3478',
    //   username: 'username',
    //   credential: 'password'
    // }
  ],
  iceCandidatePoolSize: 10
};

// 媒体约束配置
export const MEDIA_CONSTRAINTS: MediaStreamConstraints = {
  video: {
    width: { ideal: 1280, max: 1920 },
    height: { ideal: 720, max: 1080 },
    frameRate: { ideal: 30, max: 60 }
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    autoGainControl: true
  }
};

// 房间配置
export const ROOM_CONFIG = {
  MAX_PARTICIPANTS: 2, // MVP 版本支持 2 人
  ROOM_EXPIRY_HOURS: 24, // 房间 24 小时后过期
  CLEANUP_DELAY_MINUTES: 5, // 最后一人离开后 5 分钟清理房间
  MAX_ROOM_ID_LENGTH: 36,
  MIN_ROOM_ID_LENGTH: 6
};

// Socket 事件类型
export const SOCKET_EVENTS = {
  // 房间相关
  JOIN_ROOM: 'join-room',
  LEAVE_ROOM: 'leave-room',
  ROOM_JOINED: 'room-joined',
  ROOM_LEFT: 'room-left',
  USER_JOINED: 'user-joined',
  USER_LEFT: 'user-left',
  ROOM_FULL: 'room-full',
  ROOM_NOT_FOUND: 'room-not-found',
  
  // WebRTC 信令
  OFFER: 'offer',
  ANSWER: 'answer',
  ICE_CANDIDATE: 'ice-candidate',
  
  // 连接状态
  CONNECT: 'connect',
  DISCONNECT: 'disconnect',
  CONNECT_ERROR: 'connect_error',
  
  // 媒体控制
  TOGGLE_AUDIO: 'toggle-audio',
  TOGGLE_VIDEO: 'toggle-video',
  SCREEN_SHARE_START: 'screen-share-start',
  SCREEN_SHARE_STOP: 'screen-share-stop'
} as const;

// 错误消息
export const ERROR_MESSAGES = {
  CAMERA_PERMISSION_DENIED: '摄像头权限被拒绝，请在浏览器设置中允许访问摄像头',
  MICROPHONE_PERMISSION_DENIED: '麦克风权限被拒绝，请在浏览器设置中允许访问麦克风',
  MEDIA_DEVICE_NOT_FOUND: '未找到可用的摄像头或麦克风设备',
  WEBRTC_NOT_SUPPORTED: '您的浏览器不支持 WebRTC，请使用最新版本的 Chrome、Firefox 或 Safari',
  NETWORK_ERROR: '网络连接错误，请检查您的网络连接',
  ROOM_NOT_FOUND: '房间不存在或已过期',
  ROOM_FULL: '房间已满，无法加入',
  CONNECTION_FAILED: '连接失败，请重试',
  UNKNOWN_ERROR: '发生未知错误，请刷新页面重试'
};

// 成功消息
export const SUCCESS_MESSAGES = {
  ROOM_CREATED: '房间创建成功',
  ROOM_JOINED: '成功加入房间',
  LINK_COPIED: '房间链接已复制到剪贴板',
  CALL_STARTED: '通话已开始',
  CALL_ENDED: '通话已结束'
};

// 路由路径
export const ROUTES = {
  HOME: '/',
  ROOM: '/room/:roomId',
  ERROR: '/error'
};

// 本地存储键名
export const STORAGE_KEYS = {
  USER_ID: 'facetime_user_id',
  USER_PREFERENCES: 'facetime_user_preferences',
  RECENT_ROOMS: 'facetime_recent_rooms'
};

// 重试配置
export const RETRY_CONFIG = {
  MAX_ATTEMPTS: 3,
  INITIAL_DELAY: 1000, // 1 秒
  MAX_DELAY: 10000, // 10 秒
  BACKOFF_FACTOR: 2
};

// 通话质量阈值
export const CALL_QUALITY_THRESHOLDS = {
  GOOD: {
    PACKET_LOSS_RATE: 0.02, // 2%
    RTT: 150 // ms
  },
  FAIR: {
    PACKET_LOSS_RATE: 0.05, // 5%
    RTT: 300 // ms
  }
  // 超过 FAIR 阈值则为 POOR
};

// 环境变量类型声明
declare global {
  interface ImportMetaEnv {
    VITE_API_BASE_URL?: string;
    VITE_WS_URL?: string;
    VITE_TURN_SERVER_URL?: string;
    VITE_TURN_SERVER_USERNAME?: string;
    VITE_TURN_SERVER_CREDENTIAL?: string;
  }
}